package com.hibernate.learn;

import com.hibernate.learn.models.User;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

import java.math.BigDecimal;
import java.util.List;


public class Main {

    public static void main(String[] args) {

        /*
        * Configuration data from config hibernate file
        * */
        Configuration configuration = new Configuration().configure(); // "1hibernate.cfg.xml" : only without amount
        configuration.addAnnotatedClass(User.class);

        StandardServiceRegistry registryBuilder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
        SessionFactory sessionFactory = configuration.buildSessionFactory(registryBuilder);

        // code

//        BigDecimal amount = new BigDecimal("4625625472.55");

        newUser(sessionFactory, "Alex", 16);
        newUser(sessionFactory, "Katya", 16);
        newUser(sessionFactory, "John", 25);

        for (User user : selectAllUsersDesc(sessionFactory)) {
            System.out.println(user.toString());
        }

        sessionFactory.close();

    }

    public static void newUser(SessionFactory sessionFactory, String name, int age) {
        Session session = sessionFactory.openSession();
        session.save(new User(name, age));

        session.beginTransaction();
        session.getTransaction().commit();

        session.close();
    }

    @Deprecated
    public static void newUser(SessionFactory sessionFactory, String name, int age, BigDecimal amount) {
        Session session = sessionFactory.openSession();
        session.save(new User(name, age, amount));

        session.beginTransaction();
        session.getTransaction().commit();

        session.close();
    }

    @Deprecated
    public static void newUser(SessionFactory sessionFactory, int id, String name, int age) {
        Session session = sessionFactory.openSession();
        session.save(new User(name, age, id));

        session.beginTransaction();
        session.getTransaction().commit();

        session.close();
    }

    public static User getUserClassById(SessionFactory sessionFactory, int id) {

        /*
        Session session = sessionFactory.openSession();

        User user = session.load(User.class, id);

        session.beginTransaction().commit();
        session.close();

        return user;
        */

        //////////////////////////////////////////////////////////////

        Session sessionRead = sessionFactory.openSession();
        sessionRead.beginTransaction();

        User user = sessionRead.find(User.class, id);

        sessionRead.getTransaction().commit();
        sessionRead.close();

        return user;
    }

    public static void updateUserName(SessionFactory sessionFactory, int id, String newName) {
        Session sessionUpdateUser = sessionFactory.openSession();
        sessionUpdateUser.beginTransaction();

        User user = sessionUpdateUser.find(User.class, id);
        user.setName(newName);

        sessionUpdateUser.saveOrUpdate(user);
        sessionUpdateUser.getTransaction().commit();

        sessionUpdateUser.close();
    }

    public static void updateUserAge(SessionFactory sessionFactory, int id, int newAge) {
        Session sessionUpdateUser = sessionFactory.openSession();
        sessionUpdateUser.beginTransaction();

        User user = sessionUpdateUser.find(User.class, id);
        user.setAge(newAge);

        sessionUpdateUser.saveOrUpdate(user);
        sessionUpdateUser.getTransaction().commit();

        sessionUpdateUser.close();
    }

    public static void updateUserNameAndAge(SessionFactory sessionFactory, int id, String newName, int newAge) {
        Session sessionUpdateUser = sessionFactory.openSession();
        sessionUpdateUser.beginTransaction();

        User user = sessionUpdateUser.find(User.class, id);
        user.setName(newName);
        user.setAge(newAge);

        sessionUpdateUser.saveOrUpdate(user);
        sessionUpdateUser.getTransaction().commit();

        sessionUpdateUser.close();
    }

    public static void deleteUser(SessionFactory sessionFactory, int id) {
        Session sessionDeleteUser = sessionFactory.openSession();
        sessionDeleteUser.beginTransaction();

        User user = sessionDeleteUser.find(User.class, id);
        sessionDeleteUser.delete(user);

        sessionDeleteUser.getTransaction().commit();

        sessionDeleteUser.close();
    }

    public static List<User> selectAllUsersDesc(SessionFactory sessionFactory) {
        Session session = sessionFactory.openSession();
        session.beginTransaction();

        List<User> userList = session.createQuery("FROM Userr order by user_id desc", User.class).getResultList();

        session.getTransaction().commit();
        session.close();

        return userList;
    }

}
